#include "codemainwindow.h"

CodeMainWindow::CodeMainWindow(QWidget *parent) : QMainWindow(parent)
{
    resize(800,600);
    initMenu();
    initStatus();
    initToolBar();
    initDockWidget();
}

void CodeMainWindow::initMenu()
{
    //获取菜单栏
    mBar = this->menuBar();

    //添加菜单(焦点定位Alt+F)
    fileMenu = mBar->addMenu("文件(&F)");
    editMenu = mBar->addMenu("编辑(&E)");
    setMenu = mBar->addMenu("设置(&S)");

    //菜单上添加菜单项
    openAction = fileMenu->addAction(QIcon(":/new/prefix1/img/openfile.jpg"),"打开(&O)");
    saveAction = fileMenu->addAction(QIcon(":/new/prefix1/img/savefile.jpg"),"保存(&S)");
    selectAllAction = editMenu->addAction("全选(&A)");
    setAction = setMenu->addAction("设置1");

    //添加二级菜单
    //菜单上添加菜单
    twoMenu = fileMenu->addMenu("二级菜单");
    //二级菜单添加动作 (菜单项)
    printAct = twoMenu->addAction("打印");

    //添加分割线
    fileMenu->addSeparator();

    //菜单上添加关闭菜单项
    closeAct = fileMenu->addAction("关闭(&Q)");


    //为菜单项添加快捷方式
    openAction->setShortcut(QKeySequence("ctrl+o"));
    closeAct->setShortcut(QKeySequence("crtl+q"));

    //添加信号槽
    connect(setAction, &QAction::triggered,[=](){

    });
    connect(openAction,&QAction::triggered,[=](){
        QString filename = QFileDialog::getOpenFileName(this,"打开",
                                     "../",
                                     "ALL(*.*);;source(*.cpp *.c)",
                                     nullptr,
                                     QFileDialog::ShowDirsOnly);

        QFile file(filename);
        bool isOpen = file.open(QIODevice::ReadOnly);
        if(!isOpen)
        {
            qDebug()<<"notopen";
        }

        QByteArray text = file.readAll();
        edit->setText(text);
        file.close();
    });

}

void CodeMainWindow::initStatus()
{
    sBar = statusBar();

    //添加一个窗口部件到状态栏
    sBar->addWidget(new QLabel(tr("Ready")));

    //添加一个永久窗口部件到状态栏
    sBar->addPermanentWidget(new QLabel(tr("Copyright 2020")));

    //显示一条消息
    sBar->showMessage(tr("Ready"), 1);
}

void CodeMainWindow::initToolBar()
{
    t_bar1 = this->addToolBar("t_bar1");
    //t_bar2 = this->addToolBar("t_bar2");
    //工具栏添加动作
    t_bar1->addAction(openAction);
   // tBar->addAction(printAct);

    //设置图标与文本的相对位置
    t_bar1->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

    //设置工具栏允许停靠的区域
    t_bar1->setAllowedAreas(Qt::LeftToolBarArea);

}

void CodeMainWindow::initDockWidget()
{
    d_widget = new QDockWidget(tr("函数列表"),this);
    d_widget->setWidget(new FunListWidget);

    addDockWidget(Qt::LeftDockWidgetArea, d_widget);
    edit = new QTextEdit;
    this->setCentralWidget(edit);

   // s_widget = new QDockWidget(tr("文档视图",this);
    //addDockWidget(Qt::LeftDockWidgetArea, s_widget);

}
